package com.ruoyi.web.wuchuan.command;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.ruoyi.web.wuchuan.dto.*;
import com.ruoyi.web.wuchuan.enmu.Param;
import com.ruoyi.web.wuchuan.handle.PDFModule;
import com.ruoyi.web.wuchuan.util.PDFutil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class AfforestationGnqsybg implements PDFModule {
    private static final String pdfName = "生态适宜报告-功能区适宜报告" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".pdf";
    private static final String tempPath = System.getProperty("java.io.tmpdir");
    private final String zipName = "生态适宜报告-功能区适宜报告" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".zip";

    @Override
    public String getZipName() {
        return zipName;
    }
    @Override
    public String analyticalDataByType(PDFProperties pdfProperties, String uuid) {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(tempPath+pdfName));
            document.addTitle("example of PDF");
            document.open();
            document.add(CretePDF(pdfProperties,uuid));
        } catch (FileNotFoundException | DocumentException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
        return tempPath+pdfName;
    }
    private PdfPTable CretePDF(PDFProperties pdfProperties,String uuid) {
        PdfPTable table = new PdfPTable(Param.tittleSzls);
        table.setSpacingBefore(20f);//设置页边距
        table.setWidthPercentage(100);//设置表格宽度为100%
        PDFutil pdFutil = new PDFutil();
        //一级标题
        table.addCell(pdFutil.getCell("生态功能区生态适宜报告", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 1));
        table.addCell(pdFutil.getCell("生态功能区信息", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 0));
        BaseData baseData = pdfProperties.getStgnqxx().getBaseData();
        //生成key
        table.addCell(pdFutil.getCell("功能区切片编号", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell(baseData.getBh(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("面积(平方米)", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell(baseData.getMj(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("适宜推荐类型", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell(baseData.getSylx(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("适宜总分值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell(baseData.getSyfz(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        StgnqxxDTO stgnqxx = pdfProperties.getStgnqxx();
        List<FlfzDTO> flfzs = stgnqxx.getFlfz();
        table.addCell(pdFutil.getCell("生态分类", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("匹配分值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls , Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("加权分值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("适宜得分", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));

        flfzs.forEach(flfzDTO -> {
            table.addCell(pdFutil.getCell(flfzDTO.getStfl(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(flfzDTO.getPpfz(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(flfzDTO.getJqfz(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
            table.addCell(pdFutil.getCell(flfzDTO.getSydf(), pdFutil.getFont(Param.clofontSize, false), Param.cloSzls, Param.cloHigh, 0));
        });
        List<SyzwxxDTO> syzwxxs = pdfProperties.getSyzwxx();
        table.addCell(pdFutil.getCell("适宜植物信息", pdFutil.getFont(Param.tittleFontSize, true), Param.tittleSzls, Param.tittleHigh, 0));
        table.addCell(pdFutil.getCell("植物名称", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("所属科属", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls , Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("生态指数适宜分值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));
        table.addCell(pdFutil.getCell("适宜分值", pdFutil.getFont(Param.clofontSize, true), Param.cloSzls, Param.cloHigh, 0));

        syzwxxs.forEach(syzwxxDTO -> {
            table.addCell(pdFutil.getCell(syzwxxDTO.getZwmc(), pdFutil.getFont(15, false), Param.cloSzls, 35, 0));
            table.addCell(pdFutil.getCell(syzwxxDTO.getSsks(), pdFutil.getFont(15, false), Param.cloSzls, 35, 0));
            table.addCell(pdFutil.getCell(syzwxxDTO.getStzsfz(), pdFutil.getFont(10, false), Param.cloSzls, 35, 0));
            table.addCell(pdFutil.getCell(syzwxxDTO.getSydf(), pdFutil.getFont(15, false), Param.cloSzls, 35, 0));

        });
        return table;
    }
}
